<?php
require_once('config/config.php');
require_once('checked.php'); //判断是否登录


//思路 通过api页面 获取本地地址 将本地地址存入到cookie当中去
// http://ip-api.com/json/ 利用这个网址去获取
// 思路2 
//你更改后的值可以去修改
if($_POST){
    $success = [
        'weather'=> '',
    ];
    $code = isset($_POST['code']) ? trim($_POST['code']) :0;
    if($code){
        $sql = "SELECT name,province FROM {$pre_}region WHERE code = '$code'";
        $region = select($sql);
        $province = isset($region['province'])? $region['province'] :'';
        $city = isset($region['name'])? $region['name'] :'';
        $weather =  GetWeather($city);
        //把查询出来的值放入cookie中
        setcookie("province",$province);
        setcookie("city",$city);
        setcookie("weather",$weather);
        //现在需要获取 天气变化 和温度
        ob_end_clean();
        $success['weather'] = $weather;
        echo json_encode($success);
        exit;
    }
}
$province = isset($_COOKIE['province'])? $_COOKIE['province'] :'';
$city = isset($_COOKIE['city'])? $_COOKIE['city'] :'';
$weather = isset($_COOKIE['weather'])? $_COOKIE['weather']:'';
// 比如说 每次执行完将地址的值传递给cookie中
//默认值
if(empty($city)){
    $region =  GetLocation();
    
    //获取天气
    
    $province = isset($region['province'])? $region['province'] :'';
    $city = isset($region['name'])? $region['name'] :'';
    $weather =  GetWeather($city);
    //把查询出来的值放入cookie中
    setcookie("province",$province);
    setcookie("city",$city);
    setcookie("weather",$weather);
    
  
}



?>
<!-- 在这引入样式 和 js 文件 -->
<link rel="stylesheet" href="assets/plugins/city-picker/city-picker.css">
<script src="assets/plugins/city-picker/city-picker.data.min.js"></script>
<script src="assets/plugins/city-picker/city-picker.min.js"></script>
<style>
    /* 默认的样式 */
   .city-picker-span{
        display: inline-block;
        width:auto!important;
        background:rgba(71, 71, 71, 0)!important;
        border:0px;
        text-overflow: clip;
        white-space: nowrap;
        color:#fff!important;
        font-size:1em;
        font-weight: bold;
        padding-top:2px;
    }

    .city-picker-span > .placeholder{
        color:#fff;
    }

    .city-picker-span > .title > span{
        color:#fff;
        /* font-size: 8px; */
    }

    .city-picker-span > .arrow{
        display: none;
    }

    #weather{
        display: inline-block;
        margin-top:2px;
        margin-left:5px;
        /* top: 5px; */
        /* font-size: 8px; */
    }

    #api{
        padding-left:0;
        padding-right:0;
        margin-top: 0px;
        text-align: left;
    }
</style>
<div class="navbar">
    <div class="navbar-inner">
        <ul class="nav pull-right">
            <li>
                <a id="api" role="button">
                    <!-- data-level="city" 选择城市的级别，支持province/city/district，默认为district -->
                    <!-- data-simple="true" 使用简单的地址，比如使用内蒙古替代内蒙古自治区，默认为false -->
                    <!-- data-responsive="true" 是否为自适应，默认为false -->
                    <span href="#" id="picker" readonly data-level="city" data-simple="true" data-responsive="true" placeholder="请选择城市"></span>
                    <b id="weather">2024/09/20 多云 34度</b>
                </a>
            </li>
            <li>
                <a href="#" role="button">
                    <i class="icon-user"></i><?php echo $admin['username'] ?>
                </a>
            </li>
            <li>
                <a id="loger" href="javascript:void(0)" class="hidden-phone visible-tablet visible-desktop" role="button">Logout</a>
            </li>
        </ul>
        <a class="brand" href="index.html"><span class="second">Admin</span></a>
    </div>
</div>
<script>
    $('#loger').click(()=>{
        if(confirm('确定退出吗?')) location.href = `login.php?action=loger`;
    })
    var province =  '<?php echo $province ?>';
    var city = '<?php echo $city ?>';
    var weather = '<?php echo $weather ?>';
    $('#weather').html(weather);
    // 初始化城市的值
    $("#picker").citypicker({province,city});
    //城市切换事件
    $("#picker").on("cp:updated",function(){
        var citypicker  = $(this).data("citypicker");
        //获取市区地址码
        var code =citypicker.getCode("city");
        if(code){
            $('#weather').html('正在加载中....');
            //发送ajax请求
            $.ajax({
                type:'post',
                // 必须以对象形式传递
                data:{code},
                dataType:'json',
                success:(success) =>{
                    $('#weather').html(success.weather);
                },
                error:error =>{
                    console.log(error);
                }
            })
        } 
    })
    
</script>